package com.bdzl.biz.weChat;

import com.bdzl.biz.service.OrderCallService;
import com.bdzl.biz.service.OrderService;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.util.param.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/bdzl/wechat/order")
public class OrderWeChat extends BaseController {

    @Autowired
    private OrderCallService orderCallService;
    @Autowired
    private OrderService orderService;

    /**
     * 订单统计，总金额
     */
    @ResponseBody
    @GetMapping("/statisticsOrderSumPrice")
    public R statisticsOrderSumPrice(@RequestParam Map<String, Object> params) {
        R r = new R();
/*      融资总额，sumOdrLoanSys
        返佣总额，sumOdrReturnMoney
        奖励总额，sumOdrBonusMoney
        打款总额，sumOdrPayMoney
        利润总额，sumOdrProfitMoney
        平均毛利率，avgOdrGrossRate*/
        Map map = orderService.statisticsOrderSumPrice(params);
        BigDecimal avgOdrGrossRate = new BigDecimal(map.get("avgOdrGrossRate").toString());
        avgOdrGrossRate = avgOdrGrossRate.multiply(new BigDecimal("100"));
        avgOdrGrossRate = avgOdrGrossRate.setScale(2,BigDecimal.ROUND_DOWN);
        map.put("avgOdrGrossRate",avgOdrGrossRate+"%");
        params.put("odrZeroFlag", 'Y');//零利息
        Map map1 = orderService.statisticsOrderCount(params);
        params.put("odrZeroFlag", 'N');//非零利息
        Map map2 = orderService.statisticsOrderCount(params);
        r.put("right", map);
        r.put("zero", map1);
        r.put("nonzero", map2);
        Map map3 = new HashMap();
        params.put("odrZeroFlag", null);
        //订单总数量
        Map map4 = orderService.statisticsOrderCount(params);
        //完成定单数量
        String[] inOdrState = new String[]{"FINISH"};
        params.put("inOdrState", inOdrState);
        Map map5 = orderService.statisticsOrderCount(params);

        //逼单
        inOdrState = new String[]{"CLOSE1", "CLOSE2", "CLOSE3"};
        params.put("inOdrState", inOdrState);
        Map map7 = orderService.statisticsOrderCount(params);
        Integer totalCoun = 0;
        Integer carryCount = 0;
        Integer failureCount = 0;
        if (map4.get("orderCount") != null) {
            totalCoun = Integer.parseInt(map4.get("orderCount").toString());
        }
        if (map5.get("orderCount") != null) {
            carryCount = Integer.parseInt(map5.get("orderCount").toString());
        }
        if (map7.get("orderCount") != null) {
            failureCount = Integer.parseInt(map7.get("orderCount").toString());
        }

        //审核中订单数量 = 订单总数量 - 已完成数量 - 逼单数量；
        Integer underCount = totalCoun - carryCount - failureCount;
        map3.put("totalCoun", map4.get("orderCount"));
        map3.put("carryCount", map5.get("orderCount"));
        map3.put("underCount", underCount);
        map3.put("failureCount", map7.get("orderCount"));
        r.put("orderTotel",map3);
        return r;
    }

}
